<?php

namespace servers;

use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;

class AliPayServer
{
        protected $config = [
        'app_id' => '2021000121690743',
        'notify_url' => 'https://openapi.alipaydev.com/gateway.do',
        'return_url' => 'https://openapi.alipaydev.com/gateway.do',
            'ali_public_key'=>'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyVoeeKwB7NU11+v6pA0NK1UEhBtsAbI/xTNVA3kCxBsWFA0xkxwU2G5TS3FjhzS3CechbgpF28GYh5p5uSL86bxWCFE62aATva+GDsrpytF/jhu7YaJYbsOCx1F3zwGeo4VMbw5Oir5T3gYO0LWiHtQYrFfxMOkj49Ki3BtGoBNGh0r38VMDD5nRWKuiptwj8Jesmi6LKUTs5LXqBmkaSlp6A/0ND5e/nwZSXRj+vkhuZRD0j4zx57LFWr67d9hW5Y9qI/tqVRsmhinLoqaTgeJpKXl7y2SiN/MRTrj0fCAH1haRzsYiwoSsXLyEVnHY6fR2lNyfqb1XdmtFco5WYQIDAQAB',
        // 加密方式： **RSA2**
            'private_key'=>'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCOpUf7t4DOAxl+Q+US3GvAlWrtFREQ/lUImPP9eEOVLaALKhownQFlBhnjvNGFKvPnTpHmb00zk5/TwuWnippQkc8NociJAk+sc56W2CBAekqgJJKW3vAOoe/WmkHQfjomj2BpZ7vaJcM8YKIh/vcxwgl3Kc4EtCJaq2kQxRj0lRRwoJw2xjQpJp6zUJfDx8UDbw4WNkpiEDWBqAsePTJUBzE72Xvm+aZBmIoSHALrDSfvb8di3mqlu265WwwFY0NG25QkmxT0GA+53TZKN8j58Uz2ES7l1Fqz9VCKI4VGNzJkX8tcdcSNmev7tR+nTdz63m5sgnQ+G7MM6ZKa3x8RAgMBAAECggEAYDVl6YaI32oDxb+xPxlfq5KbfbAZxKZlX9kZpzl4C6OsMBuDcopSqHqG960ge3rO6pF5ojwri7/jqoXnMysujWzqg708bfwbFKse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xROlcT0q6QOZ3QnusfSbw6aOANm7WPxb2+yEMIOVHMjdcpYO9QlyM=',
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'debug', // 建议生产环境等级调整为 info，开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
    ];


    public function index()
    {
        $order = [
            'out_trade_no' => time(),
            'total_amount' => '1',
            'subject' => 'test subject - 测试',
        ];

        $alipay = Pay::alipay($this->config)->web($order);

        return $alipay->send();// laravel 框架中请直接 `return $alipay`
    }

     public function return()
    {
        $data = Pay::alipay($this->config)->verify(); // 是的，验签就这么简单！

        // 订单号：$data->out_trade_no
        // 支付宝交易号：$data->trade_no
        // 订单总金额：$data->total_amount
    }

    public function notify()
    {
        $alipay = Pay::alipay($this->config);

        try{
            $data = $alipay->verify(); // 是的，验签就这么简单！

            // 请自行对 trade_status 进行判断及其它逻辑进行判断，在支付宝的业务通知中，只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时，支付宝才会认定为买家付款成功。
            // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号；
            // 2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额）；
            // 3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）；
            // 4、验证app_id是否为该商户本身。
            // 5、其它业务逻辑情况

            Log::debug('Alipay notify', $data->all());
        } catch (\Exception $e) {
            // $e->getMessage();
        }

        return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
    }
}